// 提供路由规则
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

// 动态路由表 => 动态路由(需要权限才可以访问的) 我们这里准备一个数组存放
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter

]

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// import login from '@/views/login/index.vue'
// 这种组件的导入方式,是直接加载的!
// 将来用户一进入你的系统,代码执行,所有的组件全部加载进来,你会看到页面,首屏加载速度慢
// 优化用户体验,缩小首屏加载时间,希望 用户看了那个组件,只加载那个组件=>路由懒加载
// 结论:如果你希望组件变懒=>到我上次,我才加载 =>
// () => import('@/views/login/index')

export const constantRoutes = [
  // 定义路由规则
  // 一级路由  login  404...
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 首页的一级路由 且他有二级路由
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    ...constantRoutes,
    ...asyncRoutes
  ]
})
// 这里得到的就是我们的路由实例router 设置路由前置守卫
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

